
global n k casenum kmax
% This file conducts a first stage estimation of migration on the
% instruments to ascertain their relevance and strength
% Tobias Pfutze, August 2009

% Upload Data in order: pripart oppmaxpart remit_prophh_intl
% dist_border cycleB1 cycleB2 zona_norte labinc_perhh_log
% labinc_perhh_stdev illiterate indig margmuni00 poptot_2000 unempl
% raildistjuarez_05 histmig24_edoprop00 oppwin_since80 pripart1 oppmaxpart1

RAW=load('/Users/tpfutze/Dropbox/Research/Participation/Matlab/Data/participation_adjall.txt');
[N,M]=size(RAW);

% Replace total population by its logged value
RAW(:,13)=log(RAW(:,13));

% Create two separate matrices conditional on whether municipality has 
% had an opposition party in office since 1980
s0=1;
s1=1;
for i=1:N
    if (RAW(i,17)==0)
        RAW0(s0,1:M)=RAW(i,1:M);
        s0=s0+1;
    elseif RAW(i,17)==1
        RAW1(s1,1:M)=RAW(i,1:M);
        s1=s1+1;
    end
end
[N0,M0]=size(RAW0);
[N1,M1]=size(RAW1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create all matrices of dep., indep. and instr. variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create matrices and vectors 

%Define endogenous variable as the dependent one
y=RAW(:,3);
y0=RAW0(:,3);    
y1=RAW1(:,3);


z1=[ones(N,1) RAW(:,15:16) RAW(:,4:7)];                               
z2=[ones(N,1) RAW(:,15:16) RAW(:,4:14)]; 

z01=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:7)];                               
z02=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:14)];  

z11=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:7)];                               
z12=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:14)]; 


%%%%%%%%%%%%%%%%%%%
% DEFINE MATRICES %
%%%%%%%%%%%%%%%%%%%

%Matrices for point estimates and significance tests
[nmax,kmax]=size(z2);

BETA=zeros(kmax,6);    % betas for shares
F_WALD=zeros(1,6);
T_WALD=zeros(kmax,6);
F_DOF=zeros(1,6);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% PARTICIPATION RESULTS %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ENTIRE SAMPLE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=1;
[n,k] = size(z1);
Y=y;
Z=z1;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;
 

% 2ND CASE
casenum=casenum+1;
[n,k] = size(z2);
Y=y;
Z=z2;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%  CONTINUOUSLY PRI %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(z01);
Y=y0;
Z=z01;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;
 

% 2ND CASE
casenum=casenum+1;
[n,k] = size(z02);
Y=y0;
Z=z02;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% ALREADY OPPOSITION %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(z11);
Y=y1;
Z=z11;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;
 

% 2ND CASE
casenum=casenum+1;
[n,k] = size(z12);
Y=y1;
Z=z12;

% Estimation
beta=inv(Z'*Z)*Z'*Y;   % Get parameter estimates
U=Y-Z*beta;  % Get residuals

% Compute COVAR matrix
COVAR=inv((Z'*Z)*(inv(Z'*((U'*U)/n)*Z))*(Z'*Z));

% Conduct Wald test on beta parameters
[F, T] = WaldFirstStage_IVGMM(beta, COVAR); 

% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
T_WALD(1:k, casenum)=T(1:k);
F_WALD(:, casenum)=F;
F_DOF(:,casenum)=k;
clear Y Z beta F T;


%%%%%%%%%%%%%%%%%%%
% Compute p-values%
%%%%%%%%%%%%%%%%%%%

PVAL_TWALD=1-chi2cdf(T_WALD.^2,1);
PVAL_FWALD=1-chi2cdf(F_WALD,F_DOF);






save FirstStage_IVGMMResults BETA PVAL_TWALD PVAL_FWALD T_WALD F_WALD;


